//
//  LXBaseViewController.m
//  绿稀全球购
//
//  Created by 王敏 on 16/5/28.
//  Copyright © 2016年 SJT. All rights reserved.
//

#import "LXBaseViewController.h"

#import "LXNavigationController.h"

#import "NSIndexPath+LXIndexpathExtension.h"

@interface LXBaseViewController ()
@property(nonatomic,strong)UITapGestureRecognizer * searchBarTapGesture;
@end

@implementation LXBaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tabBarController.tabBar.hidden = YES;
    self.navigationItem.hidesBackButton = YES;
    [self xw_registerBackInteractiveTransitionWithDirection:XWInteractiveTransitionGestureDirectionRight transitonBlock:^(CGPoint startPoint){
        [self.navigationController popViewControllerAnimated:YES];
    } edgeSpacing:100];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationItem.hidesBackButton = YES;
    self.tabBarController.tabBar.hidden = YES;
    self.navigationController.navigationBar.hidden = NO;
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor]}];
    for (id obj in self.view.subviews) {
        if([obj isKindOfClass:[UIImageView class]]){
            UIImageView * imgv = obj;
            if(imgv.width == ScreenWidth&&imgv.height>ScreenHeight*0.7){
                [imgv removeFromSuperview];
            }
        }
    }
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.tabBarController.tabBar.hidden = YES;
    UIImageView * imgv = [[UIImageView alloc] initWithImage:[LXMallFrame screenShotWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)]];
    
    imgv.y = self.navigationController.navigationBar.hidden?0:-64;
    [self.view addSubview:imgv];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
    ((LXNavigationController*)self.navigationController).enableswipright = YES;
}



-(NSMutableDictionary*)cellHeights{
    if(_cellHeights == nil){
        _cellHeights = [NSMutableDictionary dictionary];
    }
    return _cellHeights;
}
- (void)setUpNavi{

    UINavigationBar *  navigationBar = self.navigationController.navigationBar;
    navigationBar.hidden = NO;
    navigationBar.translucent = NO;
    self.navigationController.navigationItem.leftBarButtonItem = nil;
    UIButton * backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [backButton setImage:[UIImage imageNamed:@"nav_return"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    backButton.size = CGSizeMake(19,25);
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];

    //设置导航栏背景图片
    UIImage * backgroundImage = [UIImage imageWithColor:HexColor(@"#3CB034") size:CGSizeMake(ScreenWidth, 64)];
    UIImage * shadowImage = [UIImage imageWithColor:HexColor(@"#3CB034") size:CGSizeMake(ScreenWidth, 1)];
    
    [navigationBar setBackgroundImage:backgroundImage forBarMetrics:UIBarMetricsDefault];
    [navigationBar setShadowImage:shadowImage];
    
    //导航字体颜色
    navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor]};
}
-(void)backButtonClick:(UIButton*)aButton{
    [self.navigationController popViewControllerAnimated:YES];
}


-(void)addCellHeight:(CGFloat)cellHeight indexPath:(NSIndexPath*)indexPath{
    [self.cellHeights setObject:@(cellHeight) forKey:[indexPath string]];
}

-(CGFloat)cellHeightForIndexPath:(NSIndexPath*)indexPath{
    return [self.cellHeights[[indexPath string]] floatValue];
}

-(void)addItemSize:(CGSize)itemSize indexPath:(NSIndexPath *)indexPath{
    NSValue  * value = [NSValue valueWithCGSize:itemSize];
    [self.collectionViewCellIetemsSizes setObject:value forKey:[indexPath string]];
}

-(CGSize)itemSizeForIndexPath:(NSIndexPath *)indexPath{
    NSValue * value = self.collectionViewCellIetemsSizes[[indexPath string]];
    return [value CGSizeValue];
}


-(NSMutableDictionary*)collectionViewCellIetemsSizes{
    if(_collectionViewCellIetemsSizes == nil){
        _collectionViewCellIetemsSizes = [NSMutableDictionary dictionary];
    }
    return _collectionViewCellIetemsSizes;
}


-(BOOL)shouldAutorotate{
    return NO;
}

-(UIInterfaceOrientationMask)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskPortrait;
}

-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
    return UIInterfaceOrientationPortrait;
}


//强制横屏
- (void)forceOrientationLandscape
{
    AppDelegate *appdelegate=(AppDelegate *)[UIApplication sharedApplication].delegate;
    appdelegate.isLandscape=YES;
    appdelegate.isPortrai=NO;
    [appdelegate application:[UIApplication sharedApplication] supportedInterfaceOrientationsForWindow:self.view.window];
    [[UIDevice currentDevice] setValue:@(UIInterfaceOrientationLandscapeRight) forKey:@"orientation"];
    //刷新
    [UIViewController attemptRotationToDeviceOrientation];
}

//强制竖屏
- (void)forceOrientationPortrait
{
    AppDelegate *appdelegate=(AppDelegate *)[UIApplication sharedApplication].delegate;
    appdelegate.isPortrai=YES;
    appdelegate.isLandscape=NO;
    [appdelegate application:[UIApplication sharedApplication] supportedInterfaceOrientationsForWindow:self.view.window];
    //设置屏幕的转向为竖屏
    [[UIDevice currentDevice] setValue:@(UIDeviceOrientationPortrait) forKey:@"orientation"];
    //刷新
    [UIViewController attemptRotationToDeviceOrientation];
}

@end
